﻿#region Copyright(C) 2018 FI Ltd. All rights reserved.
// ==================================================================================================
// Copyright(C) 2018 FI Ltd. All rights reserved.
//
// SYSTEM NAME	    : FI.Net Application Framework
// COMPONENT ID	    : FI.Core
// COMPONENT DESC   : FI.Net Core Foundation
//
// CREATED DATE/BY  : 18 Aug 2018 / Tian Mi
//
// REVISION HISTORY :	
// DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================
#endregion

using FI.Core.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Text;

namespace FI.Core
{
    /// <summary>
    /// Class ResponseError.
    /// </summary>
    [DataContract(Namespace = Constants.Namespace)]
    public class ErrorInfo: ICloneable
    {
        private HttpStatusCode? statusCode;

        /// <summary>
        /// Gets the error code.
        /// </summary>
        /// <value>The error code.</value>
        [DataMember]
        public string ErrorCode { get; set; }

        /// <summary>
        /// Gets the message.
        /// </summary>
        /// <value>The message.</value>
        [DataMember]
        public string Message { get; set; }


        /// <summary>
        /// Gets or sets the remark.
        /// </summary>
        /// <value>The remark.</value>
        [DataMember]
        public string Remark { get; set; }

        /// <summary>
        /// Gets or sets the URI.
        /// </summary>
        /// <value>The URI.</value>
        public string Uri { get; set; }

        /// <summary>
        /// The status code for web api.
        /// </summary>
        public HttpStatusCode StatusCode
        {
            get { return statusCode.HasValue ? statusCode.Value : HttpStatusCode.InternalServerError; }
            set { statusCode = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorInfo" /> class.
        /// </summary>
        /// <param name="errorCode">The error code.</param>
        /// <param name="message">The message.</param>
        /// <param name="remark">The remark.</param>
        /// <param name="statusCode">The status code.</param>
        public ErrorInfo(string errorCode, string message, string remark = null, HttpStatusCode? statusCode = null)
        {
            Guard.ArgumentNotNullOrEmpty(errorCode, "errorCode");
            Guard.ArgumentNotNullOrEmpty(message, "message");

            this.ErrorCode = errorCode;
            this.Message = message;
            this.Remark = remark;
            this.statusCode = statusCode;
        }

        private ErrorInfo(ErrorInfo errorInfo)
        {
            Guard.ArgumentNotNull(errorInfo, "errorInfo");
            this.ErrorCode = errorInfo.ErrorCode;
            this.Message = errorInfo.Message;
            this.Remark = errorInfo.Remark;
            this.Uri = errorInfo.Uri;
            statusCode = errorInfo.statusCode;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorInfo"/> class.
        /// </summary>
        public ErrorInfo()
        { }
        
        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>A new object that is a copy of this instance.</returns>
        public object Clone()
        {
            return new ErrorInfo(this);
        }
    }
}